
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       gcs_mavlink.h
  * @author     baiyang
  * @date       2022-4-17
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

#include <stdint.h>
#include <stdbool.h>

#include <rtdef.h>
#include <rtconfig.h>

// we have separate helpers disabled to make it possible
// to select MAVLink 1.0 in the arduino GUI build
#define MAVLINK_SEPARATE_HELPERS
#define MAVLINK_NO_CONVERSION_HELPERS

#define MAVLINK_SEND_UART_BYTES(chan, buf, len) mavcomm_send_buffer(chan, buf, len)

#define MAVLINK_START_UART_SEND(chan, size) mavcomm_send_lock(chan, size)
#define MAVLINK_END_UART_SEND(chan, size) mavcomm_send_unlock(chan)

#if CONFIG_HAL_BOARD == HAL_BOARD_SITL_WIN
// allow extra mavlink channels in SITL for:
//    Vicon, ship
#define MAVLINK_COMM_NUM_BUFFERS 7
#else
// allow five telemetry ports
#define MAVLINK_COMM_NUM_BUFFERS 5
#endif

/*
  The MAVLink protocol code generator does its own alignment, so
  alignment cast warnings can be ignored
 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"

#if defined(__GNUC__) && __GNUC__ >= 9
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
#endif

#include <c_library_v2/ardupilotmega/version.h>

#define MAVLINK_MAX_PAYLOAD_LEN 255

#include <c_library_v2/mavlink_types.h>

/// MAVLink stream used for uartA
extern rt_device_t mavlink_comm_port[MAVLINK_COMM_NUM_BUFFERS];
extern bool gcs_alternative_active[MAVLINK_COMM_NUM_BUFFERS];

/// MAVLink system definition
extern mavlink_system_t mavlink_system;

/// Sanity check MAVLink channel
///
/// @param chan		Channel to send to
static inline bool mavcomm_valid_channel(mavlink_channel_t chan)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
    return chan < MAVLINK_COMM_NUM_BUFFERS;
#pragma clang diagnostic pop
}

void mavcomm_send_buffer(mavlink_channel_t chan, const uint8_t *buf, uint8_t len);

/// Check for available transmit space on the nominated MAVLink channel
///
/// @param chan		Channel to check
/// @returns		Number of bytes available
uint16_t mavcomm_get_txspace(mavlink_channel_t chan);

#define MAVLINK_USE_CONVENIENCE_FUNCTIONS
#include <c_library_v2/ardupilotmega/mavlink.h>

// lock and unlock a channel, for multi-threaded mavlink send
void mavcomm_send_lock(mavlink_channel_t chan, uint16_t size);
void mavcomm_send_unlock(mavlink_channel_t chan);
rt_mutex_t mavcomm_chan_lock(mavlink_channel_t chan);
void mavcomm_init_chan_locks(uint8_t instance);
#pragma GCC diagnostic pop

#ifdef __cplusplus
}
#endif



